using System.Drawing;
using System.Windows.Forms;
using Toolbox.Model;

namespace Toolbox.Model.English
{
    public class EnglishPage : BaseTabPage
    {
        /// <summary>
        /// 页签名称
        /// </summary>
        public override string TabName => "常用英语";
        
        /// <summary>
        /// 配置文件名称（不含路径和扩展名）
        /// </summary>
        public override string ConfigFileName => "EnglishConfig";
        
        /// <summary>
        /// 页签序号（用于排序）
        /// </summary>
        public override int TabOrder => 2;
        
        /// <summary>
        /// 月份组控件
        /// </summary>
        private GroupBox? monthsGroup;
        
        /// <summary>
        /// 星期组控件
        /// </summary>
        private GroupBox? weekdaysGroup;
        
        /// <summary>
        /// 使用说明标签
        /// </summary>
        private Label? infoLabel;
        
        public override void InitializePage(TabPage tabPage, MainForm mainForm)
        {
            // 主面板
            var mainPanel = new Panel { Dock = DockStyle.Fill, Padding = new Padding(20) };
            tabPage.Controls.Add(mainPanel);

            // 标题
            var titleLabel = new Label
            {
                Text = "常用英语",
                Font = new Font("Microsoft YaHei", 16, FontStyle.Bold),
                Location = new Point(10, 10),
                AutoSize = true
            };
            mainPanel.Controls.Add(titleLabel);

            // 月份组
            monthsGroup = new GroupBox
            {
                Text = "月份",
                Location = new Point(10, 50),
                Size = new Size(530, 120)
            };
            mainPanel.Controls.Add(monthsGroup);

            // 月份数据
            string[] months = {
                "January (一月)", "February (二月)", "March (三月)", "April (四月)",
                "May (五月)", "June (六月)", "July (七月)", "August (八月)",
                "September (九月)", "October (十月)", "November (十一月)", "December (十二月)"
            };

            // 星期组
            weekdaysGroup = new GroupBox
            {
                Text = "星期",
                Location = new Point(10, 180),
                Size = new Size(530, 120)
            };
            mainPanel.Controls.Add(weekdaysGroup);

            // 星期数据
            string[] weekdays = {
                "Monday (星期一)", "Tuesday (星期二)", "Wednesday (星期三)", "Thursday (星期四)",
                "Friday (星期五)", "Saturday (星期六)", "Sunday (星期日)"
            };

            // 使用说明
            infoLabel = new Label
            {
                Text = "提示：\r\n1. 这里展示了月份和星期的英文表达\r\n2. 可以帮助学习和记忆常用的英语单词",
                Location = new Point(40, 310),
                Size = new Size(480, 50),
                AutoSize = false,
                TextAlign = ContentAlignment.TopLeft,
                ForeColor = Color.Gray
            };
            mainPanel.Controls.Add(infoLabel);
            
            // 初始化月份和星期标签
            ArrangeLabels(monthsGroup, months);
            ArrangeLabels(weekdaysGroup, weekdays);
            
            // 添加窗口大小改变事件处理，以便调整控件宽度和重新布局
            tabPage.Resize += (s, e) =>
            {
                // 计算新的控件宽度，统一使用20像素的左右边距
                int newWidth = mainPanel.Width - 20; // 减去左右边距
                
                // 调整月份组的宽度
                if (monthsGroup != null)
                {
                    monthsGroup.Width = newWidth;
                }
                
                // 调整星期组的宽度
                if (weekdaysGroup != null)
                {
                    weekdaysGroup.Width = newWidth;
                }
                
                // 调整使用说明标签的宽度
                if (infoLabel != null)
                {
                    infoLabel.Width = newWidth - 80; // 减去左右边距
                }
                
                // 重新排列月份和星期标签
                ArrangeLabels(monthsGroup, months);
                ArrangeLabels(weekdaysGroup, weekdays);
                
                // 更新组的高度
                UpdateGroupHeights();
                
                // 调整使用说明标签的位置
                if (infoLabel != null && monthsGroup != null)
                {
                    infoLabel.Location = new Point(40, monthsGroup.Bottom + 20);
                }
            };
        }
        
        /// <summary>
        /// 作者：huobing
        /// 功能：排列标签，实现自动计算宽度和换行
        /// </summary>
        /// <param name="container">容器控件</param>
        /// <param name="items">要显示的文本数组</param>
        private void ArrangeLabels(GroupBox? container, string[] items)
        {
            // 检查容器是否为null
            if (container == null) return;
            
            // 清除现有标签
            container.Controls.Clear();
            
            // 设置字体
            Font labelFont = new Font("Microsoft YaHei", 10);
            
            // 计算可用宽度（减去左右边距）
            int availableWidth = container.Width - 40; // 左右各20像素边距
            
            // 设置标签间距
            int labelSpacing = 20;
            
            // 当前行的X坐标和Y坐标
            int currentX = 20;
            int currentY = 30;
            
            // 当前行的最大高度
            int currentLineHeight = 0;
            
            // 创建标签
            for (int i = 0; i < items.Length; i++)
            {
                // 创建临时标签以测量文本宽度
                var tempLabel = new Label
                {
                    Text = items[i],
                    Font = labelFont,
                    AutoSize = true
                };
                
                // 测量文本宽度
                int textWidth = TextRenderer.MeasureText(tempLabel.Text, tempLabel.Font).Width;
                
                // 检查当前行是否还能放下这个标签
                if (currentX + textWidth > availableWidth && currentX > 20)
                {
                    // 换行
                    currentX = 20;
                    currentY += currentLineHeight + labelSpacing;
                    currentLineHeight = 0;
                }
                
                // 创建标签
                var label = new Label
                {
                    Text = items[i],
                    Location = new Point(currentX, currentY),
                    Size = new Size(textWidth, 20),
                    Font = labelFont
                };
                container.Controls.Add(label);
                
                // 更新当前行的X坐标和最大高度
                currentX += textWidth + labelSpacing;
                currentLineHeight = Math.Max(currentLineHeight, 20);
            }
        }
        
        /// <summary>
        /// 作者：huobing
        /// 功能：更新组的高度，根据内容自动计算
        /// </summary>
        private void UpdateGroupHeights()
        {
            // 检查控件是否为null
            if (monthsGroup == null || weekdaysGroup == null) return;
            
            // 更新月份组的高度
            int monthsHeight = 50; // 标题高度
            if (monthsGroup.Controls.Count > 0)
            {
                int maxBottom = 0;
                foreach (Control control in monthsGroup.Controls)
                {
                    maxBottom = Math.Max(maxBottom, control.Bottom);
                }
                monthsHeight = maxBottom + 20; // 添加底部边距
            }
            monthsGroup.Height = monthsHeight;
            
            // 更新星期组的位置和高度
            weekdaysGroup.Top = monthsGroup.Bottom + 20;
            
            int weekdaysHeight = 50; // 标题高度
            if (weekdaysGroup.Controls.Count > 0)
            {
                int maxBottom = 0;
                foreach (Control control in weekdaysGroup.Controls)
                {
                    maxBottom = Math.Max(maxBottom, control.Bottom);
                }
                weekdaysHeight = maxBottom + 20; // 添加底部边距
            }
            weekdaysGroup.Height = weekdaysHeight;
        }
    }
}